package com.lalifa.googledate.api

import android.content.Context
import com.drake.net.Get
import com.drake.net.Post
import com.drake.net.component.Progress
import com.drake.net.interfaces.ProgressListener
import com.lalifa.googledate.util.Tool
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.coroutineScope
import java.io.File

/**
 * 文件上传
 * @param path String
 */
suspend fun CoroutineScope.upload(path: String): String {
    return Post<BaseBean<String>>(Tool.HOST + "upload/upload") {
        param("file", File(path))
    }.await().data!!
}

/**
 * 文件上传
 * @param path String
 */
suspend fun CoroutineScope.upload(file: File): String {
    return Post<BaseBean<String>>(Tool.HOST + "upload/upload") {
        param("file", file)
    }.await().data!!
}

suspend fun CoroutineScope.upload(list: ArrayList<String>): ArrayList<String> {
    val deferredList = arrayListOf<Deferred<BaseBean<String>>>().apply {
        list.forEach {
            val deferred = Post<BaseBean<String>>(Tool.HOST + "upload/upload") {
                param("file", File(it))
            }
            add(deferred)
        }
    }
    return arrayListOf<String>().apply {
        deferredList.forEach {
            it.await().data?.let { it1 -> add(it1) }
        }
    }
}

/**
 * 下载文件
 */
suspend fun CoroutineScope.downloadFile(
    context: Context,
    name: String,
    url: String,
    callBack: (File) -> Unit
) {
    if (url.isNullOrEmpty()) {
        return
    }
    var file: File? = null
    file =
        Get<File>(url) {
            setDownloadFileName(name)
            setDownloadDir(context.filesDir.absolutePath)
            setDownloadMd5Verify()
            addDownloadListener(object : ProgressListener() {
                override fun onProgress(p: Progress) {
//                    LogUtils.i(
//                        "下载进度: ${p.progress()}% 下载速度: ${p.speedSize()}     " +
//                                "\n\n文件大小: ${p.totalSize()}  已下载: ${p.currentSize()}  剩余大小: ${p.remainSize()}" +
//                                "\n\n已使用时间: ${p.useTime()}  剩余时间: ${p.remainTime()}"
//                    )
                }
            })
        }.await()

    file?.let {
        callBack.invoke(it)
    }
}

/**
 * 登录
 */
suspend fun CoroutineScope.loginApi(token: String): BaseBean<LoginBean?> {
    return coroutineScope {
        Post<BaseBean<LoginBean?>>("user/GoogleLogin") {
            val map = HashMap<String, String>()
            map["id_token"] = token
            json(map)
        }.await()
    }
}

/**
 * 退出登录
 */
suspend fun CoroutineScope.loginOutApi(): BaseBean<Any> {
    return coroutineScope {
        Post<BaseBean<Any>>("user/logout") {
        }.await()
    }
}

/**
 * 添加事件
 */
suspend fun CoroutineScope.addEventApi(
    googleId: String,
    title: String,
    startTime: String,
    endTime: String,
    time: String,
    repeat: String,
    notice: String,
    location: String,
    illustrate: String
): BaseBean<Any> {
    return coroutineScope {
        Post<BaseBean<Any>>("user/addactivity") {
            val map = HashMap<String, String>()
            map["title"] = title
            map["start_time"] = startTime
            map["end_time"] = endTime
            map["time"] = time
            map["repeat"] = repeat
            map["notice"] = notice
            map["location"] = location
            map["illustrate"] = illustrate
            map["google_id"] = googleId
            json(map)
        }.await()
    }
}

/**
 * 隐私政策
 */
suspend fun CoroutineScope.zcApi(): BaseBean<ZcBean> {
    return Post<BaseBean<ZcBean>>("user/text") {
    }.await()
}

/**
 * 假期
 */
suspend fun CoroutineScope.holidayApi(calendarId: String): BaseBean<ZcBean> {
    return coroutineScope {
        Get<BaseBean<ZcBean>>("${Tool.GHOST}calendars/$calendarId/events?key=${Tool.webApiKey}") {
        }.await()
    }
}
